/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.app.skyview;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import javax.media.opengl.awt.GLCanvas;
import space.app.common.AbstractRender;
import space.app.common.ErrorReportEventListener;
import space.app.common.JOGLPanel;
import space.app.skyview.TextureMessage.Kind;

/**
 *
 * @author Pierre
 */
class SkyRenderer extends  AbstractRender<SkyEventListner> {

    public SkyRenderer(final JOGLPanel panel , final boolean drawBox, final float fovy,
            final boolean headup, URL box)  {
        super(new SkyEventListner(drawBox, fovy, headup, box),panel);
    }

    public void updateTexture(Kind kind, byte[] texture) {
        eventListner.updateTexture(kind, texture);
        panel.display();
    }

    public void updateFov(float fov) {
        eventListner.updateFov(fov);
        panel.display();

    }

    public void updateDrawBox(boolean db) {
        eventListner.updateDrawBox(db);
        panel.display();
    }

    public void commitView(int i, int i0) {
        eventListner.commitView(i, i0);
        panel.display();
    }

    void updateHeadUp(boolean selected) {
        eventListner.updateHeadUp(selected);
        panel.display();
    }

    void load(URL url) {
        eventListner.load(url);
        panel.display();
    }

    
    protected void buildCanvas() {
        final GLCanvas canevas = new GLCanvas();
        canevas.addGLEventListener(new ErrorReportEventListener(eventListner));
        final SkyMouseListener lsn = new SkyMouseListener();
        canevas.addMouseListener(lsn);
        canevas.addMouseMotionListener(lsn);
        canevas.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        panel.setCanvas(canevas);
    }

    class SkyMouseListener implements MouseMotionListener, MouseListener {

        int startX;
        int startY;

        public void mouseDragged(MouseEvent e) {
            commitView(e.getX() - startX, e.getY() - startY);
            startX = e.getX();
            startY = e.getY();
        }

        public void mouseMoved(MouseEvent e) {
            e.getButton();
        }

        public void mouseClicked(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            startX = e.getX();
            startY = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }
}
